﻿package code {
	// class gives players selection menu
	
	import flash.display.MovieClip;
	import com.as3toolkit.events.KeyboarderEvent;
	import com.as3toolkit.ui.Keyboarder;
	import flash.ui.Keyboard;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import caurina.transitions.Tweener;
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.errors.*;
	
	public class GUI extends MovieClip{
		private var key1:int;
		private var key2:int;
		private var key3:int;
		private var key4:int;
		private var key5:int;
		private var key6:int;
		private var key7:int;
		private var key8:int;
		private var horzCounter, vertCounter:int;
		
		private var levelNum:int;
		private var keyInfo:String;
		private var owner:Player;
		private var player:int;
		
		private var cardLayer:Sprite;
		
		public function GUI(playerNum:int, player:Player) {
			setUpKeys(playerNum);
			owner = player;
			cardLayer = new Sprite();
			addChildAt(cardLayer,0);
			levelNum = 1;
			keyInfo = "S";
		}
		
		// sets up key bindings based on player
		public function setUpKeys(playerNum:int){
			player = playerNum;
			if(playerNum == 1){
				key1 = 81; // q
				key2 = 87; // w
				key3 = 69;  // e
				key4 = 82;  // r
				key5 = 65;  // a
				key6 = 68; // d
				key7 = 83; // s
				key8 = 70; // f
			}
			else{
				key1 = 85; // u
				key2 = 73; // i
				key3 = 79; // o
				key4 = 80; // p
				key5 = 74; // j
				key6 = 76; // l
				key7 = 75; // k
				key8 = 186; // ;
			}

		}
		
		private function getLetter(keyCode:int):String{
			switch(keyCode){
				case 81: return "Q";
				break;
				
				case 87: return "W";
				break;
				
				case 69: return "E";
				break;
				
				case 82: return "R";
				break;
				
				case 65: return "A";
				break;
				
				case 68: return "D";
				break;
				
				case 83: return "S";
				break;
				
				case 70: return "F";
				break;
				
				case 85: return "U";
				break;
				
				case 73: return "I";
				break;
				
				case 79: return "O";
				break;
				
				case 80: return "P";
				break;
				
				case 74: return "J";
				break;
				
				case 76: return "L";
				break;
				
				case 75: return "K";
				break;
				
				case 186: return ";";
				break;
			}
			return ""
		}
		
		public function clearCards(){
			Tweener.addTween(cardLayer, { y: 600, alpha:1, time:.5, transition:"easeInQuint"} );
		}
		
		private function clear():void{
			if(cardLayer.numChildren > 0){
				var k:int = cardLayer.numChildren;
				while( k > 0 )
				{
					cardLayer.removeChildAt( k - 1 );
					k--;
				}
			}
		}
		
		private function showCards(array:Array, type:String):void{
			cardLayer.y += 500;
			
			for(var i:int = 0; i < array.length; i++){
				var temp:CardDisplay3 = new CardDisplay3();
				var tempImage:Bitmap = array[i].getSmallCard();
				
				if (!checkCard(i, type)) {
					temp.gotoAndStop(2);
				}
				
				switch(i){
					case 0: temp.key_txt.text = getLetter(key1);
					break;
					
					case 1: temp.key_txt.text = getLetter(key2);
					break;
					
					case 2: temp.key_txt.text = getLetter(key3);
					break;
					
					case 3: temp.key_txt.text = getLetter(key4);
					break;
					
					case 4: temp.key_txt.text = getLetter(key5);
					break;
					
					case 5: temp.key_txt.text = getLetter(key6);
					break;
					
					case 6: temp.key_txt.text = getLetter(key7);
					break;
				}
				
				
				
				temp.name_txt.text = array[i].CardName;
				temp.description_txt.text = array[i].Description;
				temp.cost_txt.text = array[i].ManaCost;
				temp.acc_txt.text = array[i].Accuracy;
				temp.eff_txt.text = array[i].Effect;
				temp.mod_txt.text = array[i].Modifier;
				
				temp.x = 80;
				temp.y = 620 - i * 50;
				
				cardLayer.addChild(temp);
				
				tempImage.x = temp.x - 25;
				tempImage.y = temp.y + 10;
				cardLayer.addChild(tempImage);
			}
			
			var lastMenu:TopMenu = new TopMenu();
			
			lastMenu.x = 80;
			lastMenu.y = 620 - i * 50;
			lastMenu.back_txt.text = String(getLetter(key8));
			cardLayer.addChild(lastMenu);
			
			Tweener.addTween(cardLayer, { y: 0, alpha:1, time:.8, transition:"easeOutQuint" } );
		}
		
		// allows player to select cards they want to play
		public function selectCard(keyCode:int){
			if(levelNum == 1){
				if(keyCode == key1){
					keyInfo = "S";
					clear();
					levelNum++;
					showCards(owner.StoredCards, "Stored");
				}
				else if(keyCode == key2){
					keyInfo = "B";
					clear();
					levelNum++;
					showCards(owner.BasicCards, "Basic");
					
				}
				else if(keyCode == key3){
					keyInfo = "H";
					clear();
					levelNum++;
					showCards(owner.HandCards, "Hand");
				}
			}
			else if(levelNum == 2){
				try{
					var index:int = 0;
					switch(keyCode){
						case key1: index = 1;
							break;
						case key2: index = 2;
							break;
						case key3: index = 3;
							break;
						case key4: index = 4;
							break;
						case key5: index = 5;
							break;
						case key6: index = 6;
							break;
						case key7: index = 7;
							break;
					}
					if(keyCode == key8){ // allows player to go back
						levelNum = 1;
						clearCards();
					}	
					else if(index >  0){
						if(keyInfo == "S"){
							if(owner.PoweredUp && index <= owner.StoredCards.length && checkMana(owner.StoredCards[index - 1])){ // can only play if powered up
								if((owner.IsAttacking && (owner.StoredCards[index - 1].Type == "attack" || owner.StoredCards[index - 1].Type == "attackN")) // checks to make sure the player is not using a card they cannot use
								   || (!owner.IsAttacking && owner.StoredCards[index - 1].Type == "defense")){ 
									owner.chooseCard(index - 1, "stored");  
								   }
							}
							trace("Powered Up: " + owner.PoweredUp + " Current Mana: " + owner.Mana + " Check Mana: " + checkMana(owner.StoredCards[index - 1]) + " Card checking: " + owner.StoredCards[index - 1].CardName + " Card Type: " + owner.StoredCards[index - 1].Type + " Owners Attack Status: " + owner.IsAttacking);
							levelNum--;
							clearCards();
						}
						else if(keyInfo == "B"){
							if(index <= owner.BasicCards.length && checkMana(owner.BasicCards[index - 1])){ // can only play if powered up
								if((owner.BasicCards[index - 1].Type != "attack" && owner.BasicCards[index - 1].Type != "defense") || (owner.IsAttacking && (owner.BasicCards[index - 1].Type == "attack" || owner.BasicCards[index - 1].Type == "attackN")) // checks to make sure the player is not using a card they cannot use
								   || (!owner.IsAttacking && owner.BasicCards[index - 1].Type == "defense")){ 
									owner.chooseCard(index - 1, "basic");
								   }
							}
							levelNum--;
							clearCards();
						}
						else if(keyInfo == "H"){
							if(index <= owner.HandCards.length && checkMana(owner.HandCards[index - 1])){ // can only play if powered up
								if((owner.HandCards[index - 1].Type != "attack" && owner.HandCards[index - 1].Type != "defense" && owner.HandCards[index - 1].Type != "attackN") ||(owner.IsAttacking && (owner.HandCards[index - 1].Type == "attack" || owner.HandCards[index - 1].Type == "attackN")) // checks to make sure the player is not using a card they cannot use
								   || (!owner.IsAttacking && owner.HandCards[index - 1].Type == "defense")){ 
									owner.chooseCard(index - 1, "hand");
								   }
							}
							levelNum--;
							clearCards();
						}
						
					}
				}
				catch(e:Error){}
			}

		}
		
		public function checkMana(testCard:Card):Boolean{
			if(owner.Mana >= testCard.ManaCost){
				return true;
			}
			return false;
		}
		
		private function checkCard(index:int, type:String):Boolean {
			if (type == "Stored") {
				if(owner.PoweredUp && index <= owner.StoredCards.length && checkMana(owner.StoredCards[index])){ // can only play if powered up
					if((owner.IsAttacking && (owner.StoredCards[index].Type == "attack" || owner.StoredCards[index].Type == "attackN")) // checks to make sure the player is not using a card they cannot use
					   || (!owner.IsAttacking && owner.StoredCards[index].Type == "defense")){ 
						return true; 
					}
				}
			}
			else if (type == "Basic") {
				if(index <= owner.BasicCards.length && checkMana(owner.BasicCards[index])){ // can only play if powered up
					if((owner.BasicCards[index].Type != "attack" && owner.BasicCards[index].Type != "defense") || (owner.IsAttacking && (owner.BasicCards[index].Type == "attack" || owner.BasicCards[index].Type == "attackN")) // checks to make sure the player is not using a card they cannot use
					   || (!owner.IsAttacking && owner.BasicCards[index].Type == "defense")){ 
						return true; 
					}
				}
			}
			else if (type == "Hand") {
				if(index <= owner.HandCards.length && checkMana(owner.HandCards[index])){ // can only play if powered up
					if((owner.HandCards[index].Type != "attack" && owner.HandCards[index].Type != "defense") ||(owner.IsAttacking && (owner.HandCards[index].Type == "attack" || owner.HandCards[index].Type == "attackN")) // checks to make sure the player is not using a card they cannot use
					   || (!owner.IsAttacking && owner.HandCards[index].Type == "defense")){ 
						return true; 
					}
				}
			}
			return false;
		}
		
	}
	
}
